package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.HomeBean;
import com.beiing.tianshuai.tianshuai.util.DateUtil;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.beiing.tianshuai.tianshuai.widget.RadiusBackgroundSpan;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by LZQ on 2017/11/17.
 */

public class HotActive_RVAdapter extends RecyclerView.Adapter<HotActive_RVAdapter.mViewHolder> {

    private Context mContext;
    private List<HomeBean.DataBean.HotActiveBean> mData;
    private LayoutInflater layoutInflater;
    private OnItemClickListener onItemClickListener;
    private OnAuthorClickListener mOnAuthorClickListener;
    private final RadiusBackgroundSpan redBackgroundSpan;

    public HotActive_RVAdapter(Context mContext, List<HomeBean.DataBean.HotActiveBean> mData) {
        this.mContext = mContext;
        this.mData = mData;
        this.layoutInflater = LayoutInflater.from(mContext);
        redBackgroundSpan = new RadiusBackgroundSpan(mContext,Color.parseColor("#FF7575"), Color.parseColor("#FF7575"), 10);
    }

    @Override
    public mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_hot_active, null);
        return new mViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final mViewHolder holder, final int position) {
        final HomeBean.DataBean.HotActiveBean bean = mData.get(position);
        if (position == 0){
            SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
            stringBuilder.append(bean.getStrech());
            stringBuilder.append(" ");
            stringBuilder.append("HOT");
            int titleLength = bean.getStrech().length();
            stringBuilder.setSpan(redBackgroundSpan, titleLength + 1, titleLength + 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            holder.activeTitle.setText(stringBuilder);
        } else {
            holder.activeTitle.setText(bean.getStrech());
        }
        Glide.with(mContext)
                .load(HttpRequestConstant.ACTIVITY_PIC_HEAD + bean.getCover())
                .into(holder.ivActiveCover);
        Glide.with(mContext)
                .load(HttpRequestConstant.USER_PIC_HEAD + bean.getPhoto())
                .into(holder.civAvatar);
        holder.civAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnAuthorClickListener != null) {
                    mOnAuthorClickListener.onAuthorClick(v, holder.getAdapterPosition(), bean);
                }
            }
        });
        holder.tvSponsor.setText(bean.getUname());
        holder.tvSponsor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnAuthorClickListener != null) {
                    mOnAuthorClickListener.onAuthorClick(v, holder.getAdapterPosition(), bean);
                }
            }
        });
        holder.tvWatchTimes.setText(bean.getView());
        holder.tvActiveTime.setText(DateUtil.getActiveTime(bean.getActionTime(), bean.getExitTime()));
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItmClick(view, (Integer) view.getTag());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class mViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.active_title)
        TextView activeTitle;
        @BindView(R.id.civ_avatar)
        CircleImageView civAvatar;
        @BindView(R.id.tv_sponsor)
        TextView tvSponsor;
        @BindView(R.id.tv_watch_times)
        TextView tvWatchTimes;
        @BindView(R.id.tv_active_time)
        TextView tvActiveTime;
        @BindView(R.id.iv_active_cover)
        ImageView ivActiveCover;

        public mViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItmClick(View view, int position);
    }

    /**
     * 头像和昵称的点击事件
     */
    public interface OnAuthorClickListener{
        void onAuthorClick(View view, int position, HomeBean.DataBean.HotActiveBean bean);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void setOnAuthorClickListener(OnAuthorClickListener onAuthorClickListener) {
        mOnAuthorClickListener = onAuthorClickListener;
    }
}
